﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sobiens.Connectors.Entities.Documents;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Xml.Serialization;

namespace Sobiens.Connectors.Entities.Settings
{
    [DataContract(Namespace = "")]
    public class AppConfiguration
    {
        #region fields

        private static DataContractSerializer _dcs = new DataContractSerializer(typeof(AppConfiguration));
        private static DataContractJsonSerializer _dcjs = new DataContractJsonSerializer(typeof(AppConfiguration));

        #endregion

        public bool Exist { get; set; }
        public string Name { get; set; }
        
        [DataMember(Order = 1)]
        public bool DetailedLogMode = true;
        
        [DataMember(Order = 2)]
        public SiteSettings SiteSettings = new SiteSettings();

        [DataMember(Order = 3)]
        public FolderSettings FolderSettings { get; set; }

        [DataMember(Order = 4)]
        public DocumentTemplates DocumentTemplates { get; set; }

        [DataMember(Order = 5)]
        public DocumentTemplateMappings DocumentTemplateMappings { get; set; }

        [DataMember(Order = 6)]
        public ExplorerConfiguration ExplorerConfiguration{ get; set; }

        [DataMember(Order = 7)]
        public ExcelConfigurations ExcelConfigurations { get; set; }

        [DataMember(Order = 8)]
        public WordConfigurations WordConfigurations { get; set; }

        [DataMember(Order = 9)]
        public OutlookConfigurations OutlookConfigurations { get; set; }

        [DataMember(Order = 10)]
        public WorkflowConfiguration WorkflowConfiguration { get; set; }

        [DataMember(Order = 11)]
        public string LanguageCode { get; set; }

        public AppConfiguration()
        {
            this.FolderSettings = new FolderSettings();
            this.ExcelConfigurations = new ExcelConfigurations();
            this.ExplorerConfiguration = new ExplorerConfiguration();
            this.WordConfigurations = new WordConfigurations();
            this.OutlookConfigurations = new OutlookConfigurations();
            this.DocumentTemplates = new DocumentTemplates();
            this.DocumentTemplateMappings = new DocumentTemplateMappings();
            this.WorkflowConfiguration = new WorkflowConfiguration();
        }

        public override string ToString()
        {
            return this.Name;
        }


        #region public methods

        public String GetXML()
        {
            return Serialize(_dcs);
        }

        public String GetJSON()
        {
            return Serialize(_dcjs);
        }

        public static AppConfiguration CreateFromJSON(String json)
        {
            return Deserialize(_dcjs, json);
        }

        public static AppConfiguration CreateFromXML(String xmlData)
        {
            return Deserialize(_dcs, xmlData);
        }

        public void ProcessNewElements()
        {

        }

        public void ProcessDeletedElements()
        {
            SiteSettings siteSettings = new Settings.SiteSettings();
            DocumentTemplates documentTemplates = new Documents.DocumentTemplates();
            DocumentTemplateMappings documentTemplateMappings = new Documents.DocumentTemplateMappings();
            siteSettings.AddRange(SiteSettings.Where(ss => ss.IsDeleted == false).ToList());
            documentTemplates.AddRange(DocumentTemplates.Where(dt => dt.IsDeleted == false).ToList());
            documentTemplateMappings.AddRange(DocumentTemplateMappings.Where(dtm => dtm.IsDeleted == false).ToList());

            SiteSettings = siteSettings;
            DocumentTemplates = documentTemplates;
            DocumentTemplateMappings = documentTemplateMappings;
        }


        #endregion

        #region private methods

        private static AppConfiguration Deserialize(XmlObjectSerializer serializer, String data)
        {
            AppConfiguration appConfig = String.IsNullOrEmpty(data)
                ? new AppConfiguration()
                : serializer.ReadObject(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(data))) as AppConfiguration;
            return appConfig;
        }

        private String Serialize(XmlObjectSerializer serializer)
        {
            MemoryStream stream = new MemoryStream();
            serializer.WriteObject(stream, this);
            return Encoding.UTF8.GetString(stream.ToArray());
        }

        #endregion
    }
}
